#!/usr/bin/env python
# coding: utf-8

# In[1]:


import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from scipy import stats 
import pymysql 
import warnings 
warnings.filterwarnings('ignore') 
plt.rcParams['font.family']='SimHei' 
plt.rcParams['axes.unicode_minus']= False


# In[32]:


# 1. 读取ab_test.csv 数据
data = pd.read_csv('./ab_data.csv')
data


# In[48]:


# 2. 计算统计量
group_data = data.groupby(['group','landing_page']).mean()['converted']
group_data


# In[5]:


# 根据常规默认值确定α ,β,K 值 α = 0.05, β = 0.2 ,K = 1 (组件样本均衡) 
alpha = 0.05 
beta = 0.2 
k =1


# In[47]:


z_alpha = stats.norm.ppf(1-alpha) 
z_beta = stats.norm.ppf(1-beta) 
# 3. 计算统计量的显著性P值
p_A = stats.norm.cdf(group_data.loc['control']['old_page'],loc=0)
p_B = stats.norm.cdf(group_data.loc['treatment']['new_page'],loc=0)
# 样本量
yangbenliang = (p_A*(1-p_A)/k+p_B*(1-p_B))*np.power((z_alpha+z_beta)/(p_A-p_B), 2)
yangbenliang


# In[43]:


pvalue = 1 - p_B
if pvalue >= alpha: 
    ptype = "无显著差异" 
else:
    ptype = "有显著差异"
ptype


# In[50]:


# 封装
def abtest(data,alpha):
    mean = data.groupby(['group','landing_page']).mean()['converted']['treatment']['new_page']
    alpha = alpha
    beta = 0.2
    k = 1
    z_alpha = stats.norm.ppf(1-alpha) 
    z_beta = stats.norm.ppf(1-beta) 
    pvalue = 1 - stats.norm.cdf(mean,loc=0)
    if pvalue >= alpha: 
        ptype = "无显著差异" 
    else:
        ptype = "有显著差异"
    return ptype,mean,pvalue
abtest(data, 0.05)


# In[ ]:




